package com.umeox.iqibla.qimi_app.protocol

import org.json.JSONObject

abstract class Protocol {

    protected var sessionId: String = "" // 会话 ID

    abstract suspend fun start()

    abstract suspend fun openAudioChannel(): Boolean

    abstract fun closeAudioChannel()

    abstract fun dispose()

    abstract suspend fun sendText(text: String)

    abstract suspend fun sendAudio(data: ByteArray)

    /**
     * {
     *   "session_id": "xxx",
     *   "type": "listen",
     *   "state": "start",
     *   "mode": "manual"
     * }
     */
    suspend fun sendStartListening() {
        val json = JSONObject().apply {
            put("session_id", sessionId)
            put("type", "listen")
            put("state", "start")
            put("mode", "manual")
        }
        sendText(json.toString())
    }

    /**
     * {
     *   "session_id": "xxx",
     *   "type": "listen",
     *   "state": "stop",
     *   "mode": "manual"
     * }
     */
    suspend fun sendStopListening() {
        val json = JSONObject().apply {
            put("session_id", sessionId)
            put("type", "listen")
            put("state", "stop")
            put("mode", "manual")
        }
        sendText(json.toString())
    }

    suspend fun sendAbortSpeaking(reason: String) {
        val json = JSONObject().apply {
            put("session_id", sessionId)
            put("type", "abort")
            put("reason", reason)
        }
        sendText(json.toString())
    }
}